package com.jeecms.wicool.dao.user.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.user.UserAccountDao;
import com.jeecms.wicool.entity.user.UserAccount;
@SuppressWarnings("unchecked")
@Repository
public class UserAccountDaoImpl extends HibernateBaseDao<UserAccount, Integer>
		implements UserAccountDao {

	@Override
	protected Class<UserAccount> getEntityClass() {
		return UserAccount.class;
	}
	@Override
	public Pagination getPage(Integer shopId,String userName,String mobile,
			int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from UserAccount bean where 1=1 ");
			if (!StringUtils.isBlank(userName)){
				f.append(" and bean.user.name like '%"+userName+"%' ");
//				f.setParam("userName",userName);
			}if (null!=shopId){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",shopId);
			}
			if(!StringUtils.isBlank(mobile)){
				f.append(" and bean.user.mobile like '%"+mobile+"%' ");
//				f.setParam("vipCardNo",vipCarNo);
			}
			if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	
	public Pagination getPage(String userName,String phoneNo,int pageNo, int pageSize,CmsUser user){
		Finder f = Finder.create("select bean from UserAccount bean where 1=1 ");
		if (!StringUtils.isBlank(userName)){
			f.append(" and bean.user.name like '%"+userName+"%' ");
		}
		
//		if (!StringUtils.isBlank(phoneNo)){
//			f.append(" and bean.user.mobile = :phoneNo");
//			f.setParam("phoneNo",Long.valueOf(phoneNo));
//		}
		if (!StringUtils.isBlank(phoneNo)){
			f.append(" and bean.user.mobile like '%"+phoneNo+"%' ");
		}
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}
	
	@Override
	public UserAccount findById(Integer id) {
		UserAccount  entity = get(id);
		return entity;
	}
	@Override
	public UserAccount save(UserAccount bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public UserAccount deleteById(Integer id) {
		UserAccount entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public List<UserAccount> getListUserAccountByUserId(Integer userId) {
		Finder f = Finder.create("select bean from UserAccount bean where bean.user.id = '"+ userId+"'");
		return find(f);
	}
	@Override
	public List<UserAccount> getListUserAccountByVipLevel(Integer vipLevel) {
		Finder f = Finder.create("select bean from UserAccount bean where bean.vipLevel = "+ vipLevel+"");
		return find(f);
	}
	@Override
	public List<UserAccount> getListUserAccountByScore(Integer beginScore,
			Integer endScore) {
		Finder f = Finder.create("select bean from UserAccount bean where bean.score >= "+ beginScore+" and bean.score <= "+ endScore);
		return find(f);
	}
	
	public void batchUpdateVipLevel(Integer beginScore,Integer endScore,String vipLevel){
		String sql = "update user_account set vip_level="+vipLevel + " where score >="+beginScore +" and score <="+endScore;
		getSession().createSQLQuery(sql).executeUpdate();
	}
	@Override
	public List<UserAccount> getListUserAccountByIds(String ids) {
		Finder f = Finder.create("select bean from UserAccount bean where bean.id in("+ ids+")");
		return find(f);
	}
	
	public void batchUpdateVipLevel(String ids,String vipLevel){
		String sql = "update user_account set vip_level="+vipLevel + " where id in("+ids +")";
		getSession().createSQLQuery(sql).executeUpdate();
	}
}